using UnityEngine;
using UnityEngine.UI;
using TEngine;
using System.Collections.Generic;
using GameLogic.Localization;
using System;
using TMPro;
using GameProtocol;
using GameConfig.ships;

namespace GameLogic
{
	partial class ShipListPanel
    {
        Action<int> m_OnShipSelected;
        IList<ShipData> m_Ships;
        int m_SelectedIndex = -1;

        public Action<int> OnShipSelected { get => m_OnShipSelected; set => m_OnShipSelected = value; }
        public int SelectedIndex { get => m_SelectedIndex; set => m_SelectedIndex = value; }

        protected override void RegisterEvent()
        {
            base.RegisterEvent();
            m_iScrollShips.SetSpawnPrefabPath((int idx) => { return "Assets/AssetRaw/UI/ShipConfigUI/ShipCardWidget.prefab"; });
            m_iScrollShips.SetRefreshFunc(OnShipScrollRefresh);
            m_iScrollShips.SetItemCountFunc(GetItemCount);

            m_btnExExpand.AddClickEvent(() => {
                if (m_IsOpen)
                    Close();
                else
                    Open();
            });
        }
        public void Refresh(IList<ShipData> shipList)
        {
            m_Ships = shipList;
            m_iScrollShips.RefreshData(false);
        }

        void OnShipScrollRefresh(int idx, GameObject obj)
        {
            var root = obj.transform.FindChildComponent<Transform>("Root");
            var shipData = m_Ships[idx];
            var shipCfg = ConfigSystem.Instance.Tables.TbShips.GetOrDefault(shipData.CfgID);
            if (shipCfg == null || shipData == null)
                return;

            var btn = root.GetComponent<ButtonEx>();
            var previewImg = root.FindChildComponent<Image>("m_imgPreview");
            var levelNameText = root.FindChildComponent<TextMeshProUGUI>("m_tmpLevelName");
            var individualNameText = root.FindChildComponent<TextMeshProUGUI>("m_tmpIndividualName");

            var bgcc = root.FindChildComponent<ColorChanger>("m_imgBG");
            var driverImg = root.FindChildComponent<Image>("m_imgDriver");
            var selectDrive = root.FindChildComponent<RectTransform>("SelectDrive");
            var selectDriveBtn = root.FindChildComponent<ButtonEx>("SelectDrive/Root");
            driverImg.gameObject.SetActive(GameDataModule.Instance.GetDriveShipIndex() == idx);
            selectDrive.gameObject.SetActive(GameDataModule.Instance.GetDriveShipIndex() != idx);
            bgcc.ChangeToIndex((m_SelectedIndex == idx ? 1 : 0));

            selectDriveBtn.AddClickEvent(() => { 
                GameDataModule.Instance.SetDriveShipIndex(idx);
                GameEvent.Send(GameEventType.OnDriveShipChanged, idx);
                m_iScrollShips.RefreshData(false);
            });

            btn.AddClickEvent(() => {
                OnItemSelected(idx);
            });
            levelNameText.text = LLocalizationManager.Instance.GetString(shipCfg.Name);
            individualNameText.text = LLocalizationManager.Instance.GetString(shipCfg.Name);
            previewImg.sprite = GameModule.Resource.LoadAsset<Sprite>(shipCfg.SpritePath);
        }
        int GetItemCount()
        {
            return m_Ships.Count;
        }

        public void OnItemSelected(int idx)
        {
            m_SelectedIndex = idx;
            m_iScrollShips.RefreshData(false);
            if(OnShipSelected != null)
                OnShipSelected.Invoke(idx);
        }
        void OnItemDriveSelected(int idx)
        {

        }
    }
}